package com.c.yinyuezhushou.Ui;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;

import com.bumptech.glide.request.transition.Transition;
import com.c.yinyuezhushou.Dialog.DownDialog;
import com.c.yinyuezhushou.Dialog.LsListDialog;
import com.c.yinyuezhushou.Fragment.CoverFragment;
import com.c.yinyuezhushou.Fragment.LyricsFragment;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.MyTool.Immersed;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.Service.MusicService;

import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.ColorFilterTransformation;
import jp.wasabeef.glide.transformations.CropTransformation;

public class PlayerActivity extends AppCompatActivity {
    private MusicItem musicItem;
    private MusicService musicService;
    private MediaPlayer mediaPlayer;
    private MyApplication myApplication;
    private LyricsFragment lyricsFragment;
    private CoverFragment coverFragment;
    private RelativeLayout relativeLayout;
    private RelativeLayout relativeLayout1;
    private RelativeLayout relativeLayout2;
    private ImageView imageView1;
    private ImageView imageView2;
    private ImageView imageView5;
    private ImageView imageView6;
    private ImageView imageView7;
    private ImageView imageView8;
    private ImageView imageView9;
    private TextView textView1;
    private TextView textView2;
    private SeekBar seekBar;
    private MultiTransformation<Bitmap> multi = new MultiTransformation<Bitmap>(new CropTransformation(300,600),new BlurTransformation(25,4),new ColorFilterTransformation(0x86000000));
    private int type = 0;

    public CoverFragment getCoverFragment() {
        return coverFragment;
    }

    public LyricsFragment getLyricsFragment() {
        return lyricsFragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        getView();

        Immersed.setImmersed(relativeLayout1,this,1);

        myApplication = (MyApplication) getApplicationContext();

        musicService = myApplication.getMusicService();
        musicItem = musicService.getMusicItem();

        coverFragment = new CoverFragment(musicItem);
        lyricsFragment = new LyricsFragment(musicItem,musicService.getMusicPlayer().getDuration());


        setAll(musicService.getMusicPlayer().getDuration());
        setall();

        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();

        ft.add(R.id.player_re2,lyricsFragment);
        ft.add(R.id.player_re2,coverFragment);

        ft.hide(lyricsFragment);
        ft.show(coverFragment);

        ft.commit();

        musicService.addOnStateChangeListenerInner(onStateChangeListenr);

    }


    public void setAll(long time){
        mediaPlayer = musicService.getMusicPlayer();
        musicItem = musicService.getMusicItem();
        seekBar.setMax((int) time);
        seekBar.setProgress(mediaPlayer.getCurrentPosition());
        seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
        textView1.setText(musicItem.getSong());
        textView2.setText(musicItem.getSinger());

        Glide.with(this).asBitmap().load(musicItem.getImageurl()).apply(RequestOptions.bitmapTransform(multi)).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                Drawable drawable = new BitmapDrawable(resource);
                relativeLayout.setBackground(drawable);
            }
        });


        if (mediaPlayer.isPlaying()){
            imageView5.setImageResource(R.drawable.xml_button_playing);
        }else{
            imageView5.setImageResource(R.drawable.xml_button_playstop);
        }

    }

    public void setall() {
        type = musicService.getPlaytype();

        if (type == 0){
            imageView7.setImageResource(R.drawable.xml_button_cycle);
        }else if (type ==1){
            imageView7.setImageResource(R.drawable.xml_button_suiji);
        }else {
            imageView7.setImageResource(R.drawable.xml_button_cycle1);
        }

        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (myApplication.getSetup().isDownAsk()){
                    DownDialog downDialog = new DownDialog(PlayerActivity.this,true,null,musicItem);
                    downDialog.show();
                }else {
                     myApplication.getDownService().downloadMusicItem(musicItem,myApplication.getSetup().getDownbr());
                    Toast.makeText(myApplication, "正在飞速下载中...", Toast.LENGTH_SHORT).show();
                }
            }
        });
        

        imageView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                musicService.start();
            }
        });

        imageView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                musicService.back();
            }
        });

        relativeLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft=getSupportFragmentManager().beginTransaction();

                ft.hide(coverFragment);
                ft.show(lyricsFragment);

                ft.commit();
            }
        });

        imageView7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                type = musicService.getPlaytype();

                if (type == 0){
                    musicService.setPlaytype(1);
                    imageView7.setImageResource(R.drawable.xml_button_suiji);
                }else if (type ==1){
                    musicService.setPlaytype(2);
                    imageView7.setImageResource(R.drawable.xml_button_cycle1);
                }else {
                    musicService.setPlaytype(0);
                    imageView7.setImageResource(R.drawable.xml_button_cycle);
                }
            }
        });

        imageView8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                musicService.next();
            }
        });

        imageView9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (musicService.getMusicItems()!=null){
                    List<MusicItem> list = musicService.getMusicItems();
                    LsListDialog lsListDialog = new LsListDialog(list,musicService);
                    lsListDialog.show(getSupportFragmentManager(),"当前播放");
                }
            }
        });
    }

    public void getView(){
        relativeLayout = findViewById(R.id.player_re);
        relativeLayout1 = findViewById(R.id.player_re1);
        relativeLayout2 = findViewById(R.id.player_re2);
        imageView1 = findViewById(R.id.player_image1);
        imageView2 = findViewById(R.id.player_image2);
        imageView5 = findViewById(R.id.player_image5);
        imageView6 = findViewById(R.id.player_image6);
        imageView7 = findViewById(R.id.player_image7);
        imageView8 = findViewById(R.id.player_image8);
        imageView9 = findViewById(R.id.player_image9);
        textView1 = findViewById(R.id.player_text1);
        textView2 = findViewById(R.id.player_text2);
        seekBar = findViewById(R.id.player_seekbar);
    }

    private MusicService.OnStateChangeListenr onStateChangeListenr = new MusicService.OnStateChangeListenr() {
        @Override
        public void onPlay(long time) {

            setAll(time);

            lyricsFragment.setRec(musicItem,time);
            coverFragment.setCir(musicItem);

        }

        @Override
        public void onPlayComplete() {

        }

        @Override
        public void onStateChange(boolean isplay, long nowtime) {
            if (isplay){
                imageView5.setImageResource(R.drawable.xml_button_playing);
                seekBar.setProgress((int) nowtime);
            }else{
                imageView5.setImageResource(R.drawable.xml_button_playstop);
                seekBar.setProgress((int) nowtime);
            }
        }
        @Override
        public void onSchedule(long nowtime) {

        }

        @Override
        public void OnTimeVariety(long nowtime) {
            seekBar.setProgress((int)nowtime);
            lyricsFragment.scroll(nowtime);
        }

        @Override
        public void loadfinised() {

        }
    };



    private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            setSchedule(seekBar.getProgress());
        }
    };

    public void setSchedule(long l){
        musicService.onSchedule(l);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        musicService.removeOnStateChangeListenerInner(onStateChangeListenr);
    }
}